package cn.cque.utils;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

/**
 * @author XWC
 */
public class JedisUtils {
	//创建连接池
	private static JedisPoolConfig config;
	private static JedisPool pool;
	
	static{
		Properties properties=new Properties();
		InputStream is= JedisUtils.class.getClassLoader().getResourceAsStream("jedis.properties");
		try {
			properties.load(is);
		} catch (IOException e) {
			e.printStackTrace();
		}
		config=new JedisPoolConfig();
		config.setMaxTotal(Integer.parseInt(properties.getProperty("maxTotal")));
		config.setMaxIdle(Integer.parseInt(properties.getProperty("maxIdle")));
		
		pool=new JedisPool(config, properties.getProperty("host"), Integer.parseInt(properties.getProperty("port")));
	}
	
	
	//获取连接的方法
	public static Jedis getJedis(){
		return pool.getResource();
	}
	
	
	//释放连接
	public static void closeJedis(Jedis j){
		j.close();
	}
}
